<template>
	<div class="layout-padding-auto layout-padding-view">
		<!-- 搜索 -->
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="账单类型">
				<el-select v-model="queryParams.billType" clearable>
					<el-option label="收入" value="1"></el-option>
					<el-option label="支出" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"
					><el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery"
					><el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>

		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div></div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData" border>
			<el-table-column prop="billAmount" label="账单金额"></el-table-column>
			<el-table-column prop="billType" label="账单类型">
				<template #default="scope">{{ scope.row.billType == '1' ? '收入' : scope.row.billType == '2' ? '支出' : '未知' }}</template>
			</el-table-column>
			<el-table-column prop="billDesc" label="账单描述"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" />
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="page.total"
			v-model:currentPage="page.currentPage"
			v-model:pageSize="page.pageSize"
			@changePage="initPage"
		></pagination>
	</div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { getPage } from '/@/api/mall/userbill';
export default defineComponent({
	props: {
		userId: {
			type: String,
			default: () => '',
		},
	},
	setup(props) {
		const data = reactive({
			queryParams: {
				billType: '',
			},
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10, // 每页显示多少条
				ascs: [],
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);

		const initPage = async () => {
			if (props.userId) {
				loading.value = true;
				let params = {
					current: data.page.currentPage,
					size: data.page.pageSize,
					ascs: data.page.ascs,
					descs: data.page.descs,
					userId: props.userId,
				};
				await getPage(Object.assign(params, data.queryParams))
					.then((response: any) => {
						data.tableData = response.data.data.records;
						data.page.total = response.data.data.total;
						loading.value = false;
					})
					.catch(() => {
						loading.value = false;
					});
			}
		};
		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = { billType: '' };
		};

		initPage();
		return {
			showSearch,
			loading,
			initPage,
			resetQuery,
			...toRefs(data),
		};
	},
});
</script>
<style scoped></style>
